$(function() {
	var brand = null;
	var stylecode = null;
	var req = null;
	var clothingcode = null;
	var brand_id = null;
	var style_code = null;
	var commonUrl = $(".commonUrl").text();

	//-----------------------------显示选择的内容--------------------------------------
	//-------------------大类--------------------------------
	$(".parent_codes").on('click', '.parent_a', function() {
		$(".0").text($(this).text());
		$(".1,.2").text(" ");
		var parent_code = $(this).next().text();
		clothingcode = $(this).next().text();
		$.ajax({
			url: commonUrl+"/brand/getClothing_class.do",
			data:'parent_code=' + parent_code + '&code='+parent_code,
			success: function(data) {
				$(".brands ul").empty();
				$(".children_codes ul").empty();
				$.each(data.clothingTypes, function(n, allVal) {
					var childcode = "";
					childcode += "<li><a href='javaScript:;' class='children_a'>";
					childcode += allVal.name;
					childcode += "</a>";
					childcode += "<span style='display: none;'>";
					childcode += allVal.code; 
					childcode += "</a></li>";
					$(".children_codes ul").append(childcode);
					$(".children_codes").show();
//					$(".ranges").detach(".lt_children_codes");
				});
				$.each(data.brands, function(n,braVal){
					var brand = "";
					brand += "<li><a href='javaScript:;' class='brand_a'>";
					brand += braVal.name;
					brand += "</a>";
					brand += "<span style='display: none;'>";
					brand += braVal.id; 
					brand += "</a></li>";
					$(".brands ul").append(brand);
				});
					$(".ma,.pb").hide();
					$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code+ "&pageindex=1&pagesize=18")
					$("#iframe").show();
			}
		});
	})

	//-------------------中类--------------------------------

	$(".children_codes").on('click', '.children_a', function() {
		$(".1").text(" > " + $(this).text());
		$(".2").text(" ");
		var parent_code = $(this).next().text();
		clothingcode = $(this).next().text();
		$.ajax({
			url: commonUrl+"/brand/getClothing_class.do",
			data:'parent_code=' + parent_code + '&code='+parent_code,
			success: function(data) {
				$(".brands ul").empty();
				$(".lt_children_codes ul").empty();
				$.each(data.brands, function(n,braVal){
					var brand = "";
					brand += "<li><a href='javaScript:;' class='brand_a'>";
					brand += braVal.name;
					brand += "</a>";
					brand += "<span style='display: none;'>";
					brand += braVal.id; 
					brand += "</a></li>";
					$(".brands ul").append(brand);
				});
				$.each(data.clothingTypes, function(n, allVal) {
					var childcode = "";
					childcode += "<li><a href='javaScript:;' class='lt_children_a'>";
					childcode += allVal.name;
					childcode += "</a>";
					childcode += "<span style='display: none;'>";
					childcode += allVal.code; 
					childcode += "</a></li>";
					$(".lt_children_codes ul").append(childcode);
					$(".lt_children_codes").show();
				});
					$(".ma,.pb").hide();
					$("#iframe").show();
					$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code+ "&pageindex=1&pagesize=18")
			}
		});
	})

	//-------------------小类--------------------------------

	$(".lt_children_codes").on('click', '.lt_children_a', function() {
		$(".2").text(" > " + $(this).text());
		var parent_code = $(this).next().text();
		clothingcode = $(this).next().text();
		$.ajax({
			url: commonUrl+"/brand/getBrandBySmallCode.do",
			data:'code='+ parent_code,
			success: function(data) {
				$(".brands ul").empty();
				$.each(data, function(n,braVal){
					var brand = "";
					brand += "<li><a href='javaScript:;' class='brand_a'>";
					brand += braVal.name;
					brand += "</a>";
					brand += "<span style='display: none;'>";
					brand += braVal.id; 
					brand += "</a></li>";
					$(".brands ul").append(brand);
				});
				$(".ma,.pb").hide();
				$("#iframe").show();
				$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code+ "&pageindex=1&pagesize=18")
			}
		});
	})

	//-------------------品牌--------------------------------

	$(".brands").on('click', '.brand_a', function() {
		$(".li_brand").replaceWith("<li class='li_brand'>" + $(this).text() + "&nbsp;<a href='javascript:;' title='删除' class='delete'>×</a>&nbsp;</li>")
		brand_id = $(this).next().text();
		$(".ma,.pb").hide();
		$("#iframe").show();
		$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code+ "&pageindex=1&pagesize=18")
	})

	//-------------------风格--------------------------------

	$(".style").on('click', '.style_a', function() {
		$(".li_style").replaceWith("<li class='li_style'>" + $(this).text() + "&nbsp;<a href='javascript:;' title='删除' class='delete'>×</a>&nbsp;</li>");
		style_code = $(this).next().text();
		$(".ma,.pb").hide();
		$("#iframe").show();
		$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code+ "&pageindex=1&pagesize=18");
	})

	//-------------删除选中的品牌或风格--------------------------

	$(".choose").on('click', '.delete', function() {
		$(this).parent().text("");
		if($(this).parent().attr("class") == "li_style"){
			style_code = null;
			$(".ma,.pb").hide();
			$("#iframe").attr("src","");
			$("#iframe").show();
			$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code+ "&pageindex=1&pagesize=18");
		}
		if($(this).parent().attr("class") == "li_brand"){
			brand_id = null;
			$(".ma,.pb").hide();
			$("#iframe").show();
			$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code+ "&pageindex=1&pagesize=18");
		}
	})

	//-------------重新请求iframe----------------------------

	/*$(".choose ul").bind('DOMSubtreeModified', function(e) {
		alert(clothingcode)
		$(".maincon,.paging").hide();
		$("#iframe").show();
		$("#iframe").attr("src",commonUrl+"/clothing/getClothes.do?"+'code='+ clothingcode + "&brand_id=" + brand_id + "&style_code=" +style_code)
	});*/

	//-----------------------------选择中的更多-----------------------------------

	$(".more").each(function(index) {

		$(this).click(function() {
			if (index == 0) {
				if ($(this).attr("data-status") == "show") {
					$(".more").eq(index).text("更多");
					$(".more").eq(index).prev().css("height", "30px");
					$(".more").eq(index).prev().css("overflow", "hidden");
					$(".children_codes").css("height", "46px");
					var speed = 200;
					$(".more").eq(index).prev().animate({
						scrollTop: 0
					}, speed);
					$(this).attr("data-status", "hidden");
					return false;
				} else {
					$(".more").eq(index).text("收起");
					$(".more").eq(index).prev().css("height", "55px");
					$(".more").eq(index).prev().css("overflow", "auto");
					$(".children_codes").css("height", "77px");
					$(this).attr("data-status", "show");
				}
			}
			if (index == 1) {
				if ($(this).attr("data-status") == "show") {
					$(".more").eq(index).text("更多");
					$(".more").eq(index).prev().css("height", "30px");
					$(".more").eq(index).prev().css("overflow", "hidden");
					$(".lt_children_codes").css("height", "46px");
					var speed = 200;
					$(".more").eq(index).prev().animate({
						scrollTop: 0
					}, speed);
					$(this).attr("data-status", "hidden");
					return false;
				} else {
					$(".more").eq(index).text("收起");
					$(".more").eq(index).prev().css("height", "55px");
					$(".more").eq(index).prev().css("overflow", "auto");
					$(".lt_children_codes").css("height", "77px");
					$(this).attr("data-status", "show");
				}
			}
			if (index == 2) {
				if ($(this).attr("data-status") == "show") {
					$(".more").eq(index).text("更多");
					$(".more").eq(index).prev().css("height", "65px");
					$(".more").eq(index).prev().css("overflow", "hidden");
					$(".brands").css("height", "76px");
					var speed = 200;
					$(".more").eq(index).prev().animate({
						scrollTop: 0
					}, speed);
					$(this).attr("data-status", "hidden");
					return false;
				} else {
					$(".more").eq(index).text("收起");
					$(".more").eq(index).prev().css("height", "99px");
					$(".more").eq(index).prev().css("overflow", "auto");
					$(".brands").css("height", "120px");
					$(this).attr("data-status", "show");
				}
			}
			if (index == 3) {
				if ($(this).attr("data-status") == "show") {
					$(".more").eq(index).text("更多");
					$(".more").eq(index).prev().css("height", "30px");
					$(".more").eq(index).prev().css("overflow", "hidden");
					$(".style").css("height", "46px");
					var speed = 200;
					$(".more").eq(index).prev().animate({
						scrollTop: 0
					}, speed);
					$(this).attr("data-status", "hidden");
					return false;
				} else {
					$(".more").eq(index).text("收起");
					$(".more").eq(index).prev().css("height", "55px");
					$(".more").eq(index).prev().css("overflow", "auto");
					$(".style").css("height", "77px");
					$(this).attr("data-status", "show");
				}
			}
		})
	})
});